import 'package:flutter/material.dart';

import '../../services/ScreenAdatper.dart';
import 'package:provider/provider.dart';
import '../../provider/Cart.dart';
import '../Cart/CartItem.dart';

class CartPage extends StatefulWidget {
  CartPage({Key key}) : super(key: key);

  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  bool _isEdit = false;
  @override
  Widget build(BuildContext context) {
    ScreenAdatper.init(context);
    var cartProvider = Provider.of<Cart>(context);
    return Scaffold(
      appBar: AppBar(title: Text("购物车"), actions: [
        IconButton(
          icon: Icon(Icons.edit),
          onPressed: (){
            setState(() {
              this._isEdit = !this._isEdit;
              cartProvider.checkAll(!this._isEdit);
            });
          },
        )
      ]),
      body: Stack(
        children: <Widget>[
          ListView(
            padding: EdgeInsets.only(bottom: ScreenAdatper.height(78)),
            children: cartProvider.cartList.map((item){
                return  CartItem(item);
            }).toList()
          ),
          Positioned(
            bottom: 0,
            width: ScreenAdatper.width(750),
            height: ScreenAdatper.height(78),
            child: Container(
              decoration: BoxDecoration(color: Colors.white,border:Border(top: BorderSide(width: 1, color: Colors.black12))),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Checkbox(
                        value: cartProvider.isCheckAll,
                        activeColor: Colors.pink,
                        onChanged: (value) {
                          cartProvider.checkAll(value);
                        },
                      ),
                      Text("全选"),
                      SizedBox(width: 10,),
                      this._isEdit ? Text("") : Text("合计：${cartProvider.allPrice}")
                    ],
                  ),
                  Container(
                    width: ScreenAdatper.height(180),
                    height: ScreenAdatper.height(78),
                    child: RaisedButton(
                        child: Text(
                          "${this._isEdit ? '删除' : '结算'}",
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Colors.white),
                        ),
                        color: Colors.red,
                        textColor: Colors.white,
                        onPressed: () {
                          if(this._isEdit){
                              cartProvider.removeItem();
                          }
                          print("object");
                          print(this._isEdit);
                        }),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
